
// RigCalibDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RigCalib.h"
#include "RigCalibDlg.h"
#include "gui_size_macro.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif



// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CRigCalibDlg dialog




CRigCalibDlg::CRigCalibDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRigCalibDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRigCalibDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RIGCALIB_TAB, m_mainTabCtrl);
}

BEGIN_MESSAGE_MAP(CRigCalibDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_NOTIFY(TCN_SELCHANGE, IDC_RIGCALIB_TAB, &CRigCalibDlg::OnTcnSelchangeRigcalibTab)
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// CRigCalibDlg message handlers

BOOL CRigCalibDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	MoveWindow(50, 50, 1024, 900);

	m_mainTabCtrl.MoveWindow(0, 0, TAABLE_CONTRL_WIDTH, TABLE_CONTRL_HEIGHT);
	
	//add items to table contrl
	TCITEM   item; 
	//side by side
	item.mask      =   TCIF_TEXT; 
	item.pszText   =   "Side by Side";
	m_mainTabCtrl.InsertItem(SideBySide, &item);
	m_sideBySide.Create(IDD_SIDE_BY_SIDE, &m_mainTabCtrl);  //init side by sid
	m_sideBySide.SetWindowPos(NULL, 0, 0, PAGE_WIDTH, PAGE_HEIGHT, SWP_SHOWWINDOW); 

	//opinions
	item.mask = TCIF_TEXT;
	item.pszText = "Opinions";
	m_mainTabCtrl.InsertItem(Opinions, &item);	
	m_options.Create(IDD_OPINIONS, &m_mainTabCtrl);	 //init opinions
	m_options.SetWindowPos(NULL, 0, 0, PAGE_WIDTH, PAGE_HEIGHT, SWP_HIDEWINDOW);

	//overlay
	item.mask = TCIF_TEXT;
	item.pszText = "Overlay";
	m_mainTabCtrl.InsertItem(Overlay, &item);
	m_overlay.Create(IDD_OVERLAY, &m_mainTabCtrl);
	m_overlay.SetWindowPos(NULL, 0, 0, PAGE_WIDTH, PAGE_HEIGHT, SWP_HIDEWINDOW);

	//create CMechAlignDlg
	m_mechAlignDlg.Create(IDD_MECH_ALIGN, this);
	m_mechAlignDlg.SetWindowPos(NULL, 0, TABLE_CONTRL_HEIGHT+10, 1024, 300, SWP_SHOWWINDOW);
	m_mechAlignDlg.SetUpdataDialog(&m_sideBySide);
	m_mechAlignDlg.SetOverLayDialog(&m_overlay);


	//set opinion 
	CString deviceName = m_inputDevice.GetModeName();
	m_options.AddDeviceName(deviceName);

	
	//opinions to inputCallback;
	CvSize size = m_options.GetSize();
	bool isStereo = m_options.Binocular();
	int rightFlip = m_options.GetRightFlipFlag();
	int nchannels = m_options.GetChannels();
	m_inputCallback.SetResoltion(size.width, size.height, isStereo);
	m_inputCallback.SetRightFlip(rightFlip);

	//sidebysideDlg to callback
	m_inputCallback.SetUpdataDialog(&m_mechAlignDlg);

	//set callback to inputDevice;
	m_inputDevice.SetCallback(&m_inputCallback);

	//opinions to inputDevice	
	BMDDisplayMode displayMode;
	BMDPixelFormat pixelFormat;
	int flag;
	m_options.GetFormat(displayMode, pixelFormat, flag);	
	m_inputDevice.StartStream(displayMode, pixelFormat, flag);	

	


	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CRigCalibDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CRigCalibDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRigCalibDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CRigCalibDlg::OnTcnSelchangeRigcalibTab(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: Add your control notification handler code here
	CRect   r; 
	m_mainTabCtrl.GetClientRect(&r); 

	switch(m_mainTabCtrl.GetCurSel()) 
	{ 
	case SideBySide:
		{
			m_sideBySide.ShowWindow(SW_SHOW);
			m_overlay.ShowWindow(SW_HIDE);
			m_options.ShowWindow(SW_HIDE);

			//change callback
			CvSize size = m_options.GetSize();
			bool isStereo = m_options.Binocular();
			int nchannels = m_options.GetChannels();
			float fps = m_options.GetFps();
			int rightFlip = m_options.GetRightFlipFlag();
			m_inputCallback.SetResoltion(size.width, size.height, isStereo);			
			m_inputCallback.SetFps(fps);
			m_inputCallback.SetRightFlip(rightFlip);

			//change inputDevice
			BMDDisplayMode displayMode;
			BMDPixelFormat pixelFormat;
			int flag;
			m_options.GetFormat(displayMode, pixelFormat, flag);

			
			m_inputDevice.StartStream(displayMode, pixelFormat, flag);

			//change MechAlign
			bool isStartCalib = m_options.startCalib();
			bool isDenseCalib = m_options.isDenseCalib();
			if (isStartCalib && isDenseCalib)
			{
				SCameraConfigure left;
				SCameraConfigure right;
				m_options.GetCameraConfigure(left, right);
				m_mechAlignDlg.SetCameraConfigure(left, right, size.width, size.height);				
			}
			m_mechAlignDlg.EnableSideBySide();
			m_mechAlignDlg.DisableOverlay();
			m_mechAlignDlg.DisableShowIA();

		}		
		
		break; 
	case Opinions:		
		m_sideBySide.ShowWindow(SW_HIDE);
		m_overlay.ShowWindow(SW_HIDE);
		m_options.ShowWindow(SW_SHOW);
		m_inputDevice.StopStream();

		//disable calib
		m_mechAlignDlg.DisableCalib();
		m_mechAlignDlg.DisableSideBySide();
		m_mechAlignDlg.DisableOverlay();
		m_mechAlignDlg.DisableShowIA();
		m_mechAlignDlg.DisableDenseCalib();

		break;
	case Overlay:
		{
			m_sideBySide.ShowWindow(SW_HIDE);
			m_overlay.ShowWindow(SW_SHOW);
			m_options.ShowWindow(SW_HIDE);

			//change callback
			CvSize size = m_options.GetSize();
			bool isStereo = m_options.Binocular();
			int nchannels = m_options.GetChannels();
			int rightFlip = m_options.GetRightFlipFlag();
			float fps = m_options.GetFps();
			m_inputCallback.SetResoltion(size.width, size.height, isStereo);
			m_inputCallback.SetFps(fps);
			m_inputCallback.SetRightFlip(rightFlip);

			//change inputDevice
			BMDDisplayMode displayMode;
			BMDPixelFormat pixelFormat;
			int flag;
			m_options.GetFormat(displayMode, pixelFormat, flag);

			m_inputDevice.StartStream(displayMode, pixelFormat, flag);

			//change MechAlign camera parameter
			bool isStartCalib = m_options.startCalib();
			bool isDenseCalib = m_options.isDenseCalib();
			if (isStartCalib || isDenseCalib)
			{
				SCameraConfigure left;
				SCameraConfigure right;
				m_options.GetCameraConfigure(left, right);
				m_mechAlignDlg.SetCameraConfigure(left, right, size.width, size.height);

				if (m_options.isShowIA())
				{
					m_mechAlignDlg.EnableShowIA();
				}
			}

			//change dense calibration			
			if (isDenseCalib)
			{
				m_mechAlignDlg.EnableDenseCalib();
			}
			m_mechAlignDlg.DisableSideBySide();
			m_mechAlignDlg.EnableOverlay();
		}
		
		break; 
	} 


	*pResult = 0;
}

void CRigCalibDlg::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	m_inputDevice.StopStream();
	CDialog::OnClose();
}
